package hibu.LeiJonGun.seniorservice;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/* 
 * Written by: Henrik Leinaas, Elis Tor Jonsson and Christian L. Gunvaldsen
 * 
 * Stores all data about the assignment
 */
public class Assignment {
	private int assignmentID; // Is generated in the database
	private String assignmentText;
	private User assistant = null;
	private String category;
	private User employer;
	private Timestamp finishedTime = null;
	private Timestamp startTime;
	private Timestamp takenTime = null;
	private String userdefinedText; 
	
	// Use this constructor if there is noe userdefinedText:
	public Assignment(int assignmentID, User employer, 
			String category, Timestamp startTime, String assignmentText) throws Exception {
		
		//Data check:
		if (assignmentID < 0) throw new Exception("Wrong value on ID");
		if (employer==null) throw new Exception("Employer cant be empty");
		if (category == null) throw new Exception("Assignment cant be empty");
		if (startTime == null) throw new Exception("Starttime cant be empty");
		
		this.assignmentID=assignmentID;
		this.employer=employer;
		this.category=category;
		this.startTime=startTime;
		this.assignmentText=assignmentText;
		userdefinedText="";
	}
	
	// Use this constructor if there is a userdefinedText:
	public Assignment(int assignmentID, User employer, 
			String category, Timestamp startTime, String assignmentText, String userdefinedText) throws Exception {
		
		//Data check:
		if (assignmentID < 0) throw new Exception("Wrong value on ID");
		if (employer==null) throw new Exception("Employer cant be empty");
		if (category == null) throw new Exception("Assignment cant be empty");
		if (startTime == null) throw new Exception("Starttime cant be empty");
		
		this.assignmentID=assignmentID;
		this.employer=employer;
		this.category=category;
		this.startTime=startTime;
		this.assignmentText=assignmentText;
		this.userdefinedText=userdefinedText;
	}
	
	//Convert Date to String, to store in DB:
	public String convertDateToDBTime(Date c){ 
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault());
		String timedate = df.format(c.getTime());
		return timedate;
	}
	
	public int getAssignmentID() {return assignmentID;}
	
	public String getAssignmentText() {return assignmentText;}
	
	public User getAssistant() {return assistant;}
	
	public String getCategory() {return category;}
	
	public String getDescription() {return userdefinedText;}
	
	public User getEmployer() {return employer;}
	
	public Timestamp getFinishedTime() {return finishedTime;}
	
	public Timestamp getStartTime() {return startTime;}
	
	public Timestamp getTakenTime() {return takenTime;}
	
	public boolean isFinished() {
		if (finishedTime != null) return true;
		else return false;
	}
	
	public boolean isFree() {
		if (takenTime == null && finishedTime == null) return true;
		else return false;
	}
	
	public boolean isTakenAndNotFinished() {
		if (takenTime != null && finishedTime == null) return true;
		else return false;
	}
	
	public void setFinishedTime(Timestamp finishedTime) throws Exception {
		if (finishedTime==null) throw new Exception("Finishtime cant be empty");
		this.finishedTime=finishedTime;
	}
	
	public void setID(int id){this.assignmentID = id;}
	
	// Set assistant AND taken time:
	public void setAssignmentTaken(User assistant, Timestamp takenTime) throws Exception {
		if (assistant==null) throw new Exception("Assistent cant be empty");
		if (takenTime == null) throw new Exception("Takentime cant be empty");
		this.assistant=assistant;
		this.takenTime=takenTime;
	}	
}
